<template>
  <div id="cluereport">
    <el-dialog title="选择单位" :visible.sync="exportVisible" width="500px">
      <div style="margin-left:20px">
        <el-checkbox v-for="(item,index) in teams" :key="'team'+index" v-model="unitType" :label="index+1">{{item}}</el-checkbox>
        <el-button type="primary" size="small" style="margin-left:10px;" @click.native="exportCustom">导出</el-button>
      </div>
    </el-dialog>
    <el-dialog
      title="退回"
      :visible.sync="formShowVisible"
      width="98%" top="20px" :close-on-click-modal="false" :close-on-press-escape="false">
      <el-form label-width="200px" ref="editForm" style="padding:10px 0 0;clear:both">
        <el-form-item label="单位名称">
          {{unitName}}
        </el-form-item>
        <el-form-item prop="backReason" label="退回原因">
          <el-input
            v-model="backReason"
            style="width:400px;"
            :maxlength="200"
          ></el-input>
        </el-form-item>
        <el-form-item>
          <el-button type="primary" @click.native="backSubmit()" :loading="submitLoading">退回</el-button>
        </el-form-item>
      </el-form>
    </el-dialog>
    <el-breadcrumb separator=">">
      <el-breadcrumb-item :to="{ path: '/' }">系统首页</el-breadcrumb-item>
      <el-breadcrumb-item :to="{ path: '/unitReport' }">统计填报</el-breadcrumb-item>
      <el-breadcrumb-item >填报情况</el-breadcrumb-item>
    </el-breadcrumb>
    <section class="wboxs">
    <!--工具条-->
      <h1>项目名称：{{project.title}}</h1>
      <template v-if="inRoles('ROLE_jijianbianzhi')||project.mine">
        <el-button v-if="!project.customType&&project.mine" type="primary" @click="exportList"  icon="el-icon-download" style="float:right;margin:10px;">导出单位上报情况表</el-button>
        <el-button v-if="!project.customType&&project.mine" type="success" @click="exportDataList"  icon="el-icon-download" style="float:right;margin:10px;">导出本项目全部填报数据</el-button>
        <el-button v-if="project.customType" type="success" @click="selectTeams"  icon="el-icon-download" style="float:right;margin:10px;">导出本项目全部填报数据</el-button>
      </template>
      <div style="line-height:200%;margin:10px;">填报起止日期：{{project.beginDate}} 至 {{project.endDate}}<br>{{project.remark}}</div>
    </section>
    <section class="unitbox" style="margin-top:10px">
    <!-- <section style="margin-top:10px"> -->
      <!--列表-->
      <el-table :data="list" highlight-current-row v-loading="listLoading" style="width: 100%;" @sort-change='sortChange' :height="tableHeight" border
        @selection-change="handleSelectionChange"
        ref="multipleTable"
        >
        <el-table-column type="index" label="序号" width="60" align="center" class-name="tableth">
        </el-table-column>
        <el-table-column label="单位名称" prop="unitName" show-overflow-tooltip>
        </el-table-column>
        <el-table-column label="状态" prop="state" width="100">
          <template slot-scope="scope">
            <div v-html="stateText(scope.row.state)" />
          </template>
        </el-table-column>
        <el-table-column label="上报条数" prop="dataNum" width="90" v-if="!project.customType">
          <template slot-scope="scope">
            {{scope.row.state===1?scope.row.itemSize:''}}
          </template>
        </el-table-column>
        <el-table-column label="附件个数" prop="filesNum" width="90" v-if="!project.customType" >
          <template slot-scope="scope">
            {{scope.row.state===1?scope.row.filesNum:''}}
          </template>
        </el-table-column>
        <el-table-column label="上报时间" prop="submitDate" width="180">
        </el-table-column>
        <el-table-column label="退回时间" prop="backTime" width="180">
        </el-table-column>
        <el-table-column label="退回原因" prop="backReason" width="200" show-overflow-tooltip>
        </el-table-column>
        <el-table-column v-if="project.mine" label="导出时间" prop="exportTime" width="180">
        </el-table-column>
        <el-table-column label="操作" width="250" align="center">
          <template slot-scope="scope">
            <el-button v-if="!project.customType&&scope.row.state===1" @click="detail(scope.row.projectDataId)" type="primary" size="mini" style="margin:10px 0 10px">查看</el-button>
            <el-button v-if="project.customType&&scope.row.state===1" @click="customDetail(scope.row.projectDataId)" type="primary" size="mini" style="margin:10px 0 10px">查看</el-button>
            <el-button v-if="!project.customType&&scope.row.state===1" @click="exportUnitData(scope.row.projectDataId)" type="success" size="mini">导出</el-button>
            <el-button v-if="scope.row.state===1" type="danger" @click="backData(scope.row.unitName,scope.row.projectDataId)" size="mini">退回</el-button>
          </template>
        </el-table-column>
      </el-table>
      <!-- <div class="block">
        <el-pagination style="float:right" :total="totalnum" :page-size="pageSize" @current-change="handleCurrentChange" :current-page="pageNum" @size-change="handleSizeChange" :page-sizes="[10, 20, 30, 50]" layout="total, sizes, prev, pager, next, jumper"></el-pagination>
      </div> -->
    </section>
  </div>
</template>

<script>
import formShow from './formShow'
export default {
  components: {
    formShow
  },
  data () {
    return {
      exportVisible: false,
      unitType: [1, 2, 3],
      teams: ['机关司局', '派出机构', '直属单位'],
      project: {},
      listLoading: true,
      tableHeight: 100,
      list: [
      ],
      changeSizeFlag: false,
      unitName: '',
      projectDataId: '',
      backReason: '',
      formShowVisible: false,
      submitLoading: false
    }
  },
  methods: {
    inRoles (str) {
      let arr = JSON.parse(localStorage.getItem('Roles'))
      // arr = 'aaabbROLE_admin'
      if (arr.indexOf(str) === -1) {
        return false
      } else {
        return true
      }
    },
    selectTeams () {
      if (this.project.mine) {
        this.exportVisible = true
      } else {
        if (localStorage.getItem('unitName') === '机构编制处') {
          this.exportVisible = true
        } else {
          let team = null
          if (localStorage.getItem('unitName') === '干部一处') {
            team = 1
          } else if (localStorage.getItem('unitName') === '干部二处') {
            team = 3
          } else if (localStorage.getItem('unitName') === '干部三处') {
            team = 2
          }
          window.open(this.$http.defaults.baseURL + this.$http.defaults.unitReportAPI + 'api/custom/exportProjectById2025?id=' + this.$route.params.id + '&unitType=' + team)
        }
      }
    },
    exportCustom () {
      if (this.unitType.length) {
        if (this.project.flag2025) {
          window.open(this.$http.defaults.baseURL + this.$http.defaults.unitReportAPI + 'api/custom/exportProjectById2025?id=' + this.$route.params.id + '&unitType=' + this.unitType.join(','))
        } else {
          window.open(this.$http.defaults.baseURL + this.$http.defaults.unitReportAPI + 'api/custom/exportProjectById?id=' + this.$route.params.id + '&unitType=' + this.unitType.join(','))
        }
      } else {
        this.$message({
          message: '至少选择一支队伍',
          type: 'error'
        })
      }
    },
    exportDataList () {
      window.open(this.$http.defaults.baseURL + this.$http.defaults.unitReportAPI + 'api/project/exportProjectById?id=' + this.$route.params.id)
      setTimeout(() => {
        this.getList()
      }, 5000)
    },
    exportUnitData (id) {
      window.open(this.$http.defaults.baseURL + this.$http.defaults.unitReportAPI + 'api/project/exportProjectDataById?id=' + id)
      setTimeout(() => {
        this.getList()
      }, 5000)
    },
    detail (id) {
      this.$router.push('/unitReport/unitProjectDetail/' + id)
    },
    customDetail (id) {
      if (this.project.flag2025) {
        this.$router.push('/unitReport/customUnitProjectDetail2025/' + id)
      } else {
        this.$router.push('/unitReport/customUnitProjectDetail/' + id)
      }
    },
    backSubmit () {
      if (!this.backReason || this.backReason === '') {
        this.$message({
          message: '请填写退回原因',
          type: 'error'
        })
        return false
      }
      this.submitLoading = true
      let para = {
        backReason: this.backReason,
        projectDataId: this.projectDataId
      }
      this.$http
        .post(this.$http.defaults.unitReportAPI + `api/project/saveBackInfo`, para)
        .then(response => {
          if (response.data.success) {
            this.$message({
              message: '退回操作成功！',
              type: 'success'
            })
            this.formShowVisible = false
            this.getList()
          } else {
            this.$message({
              message: '退回失败:' + response.data.message,
              type: 'error'
            })
          }
          this.submitLoading = false
        })
        .catch(error => {
          this.submitLoading = false
          this.$message({
            message: '网络提交失败:' + error,
            type: 'error'
          })
        })
    },
    backData (unitName, projectDataId) {
      this.unitName = unitName
      this.projectDataId = projectDataId
      this.formShowVisible = true
    },
    exportList () {
      window.open(this.$http.defaults.baseURL + this.$http.defaults.unitReportAPI + 'api/project/exportProjectDataList?projectId=' + this.$route.params.id)
    },
    stateText (state) {
      switch (state) {
        case 0:
          return '未上报'
        case 1:
          return '<span style="color:green;font-weight:bolder"><i class="el-icon-success" /> 已上报</span>'
        case 2:
          return '<span style="color:red;font-weight:bolder"><i class="el-icon-error" /> 已退回</span>'
      }
    },
    handleSelectionChange (val) {
      this.ids = val
    },
    handleSizeChange (val) {
      this.changeSizeFlag = true
      this.pageNum = 1
      this.pageSize = val
      this.getList()
    },
    handleCurrentChange (val) {
      if (this.changeSizeFlag === false) {
        this.pageNum = val
        this.getList()
      }
    },
    // 排序
    sortChange: function (column, prop, order) {
      this.Sortkey = column.prop
      if (column.order === 'ascending') {
        this.Isace = 'asc'
      } else {
        this.Isace = 'desc'
      }
      this.getList()
    },
    getList () {
      this.listLoading = true
      this.$http
        .get(this.$http.defaults.unitReportAPI + `api/project/getProjectById?id=` + this.$route.params.id)
        .then(response => {
          this.project = response.data.result
        })
        .catch(error => {
          this.$message({
            message: '网络获取失败:' + error,
            type: 'error'
          })
        })
      this.$http
        .get(this.$http.defaults.unitReportAPI + `api/project/getProjectDataList?projectId=` + this.$route.params.id)
        .then(response => {
          this.list = response.data.result
          this.listLoading = false
        })
        .catch(error => {
          this.listLoading = false
          this.$message({
            message: '网络获取失败:' + error,
            type: 'error'
          })
        })
    },
    tableresize (obj) {
      var clientHeight = 0
      if (document.body.clientHeight && document.documentElement.clientHeight) {
        clientHeight = (document.body.clientHeight < document.documentElement.clientHeight) ? document.body.clientHeight : document.documentElement.clientHeight
      } else {
        clientHeight = (document.body.clientHeight > document.documentElement.clientHeight) ? document.body.clientHeight : document.documentElement.clientHeight
      }
      let myheight = 275

      obj.$nextTick(function () {
        obj.tableHeight = clientHeight - myheight
      })
    }
  },
  mounted () {
    this.getList()
    this.tableresize(this)
    window.onresize = () => {
      this.tableresize(this)
    }
  }
}
</script>
<style>
#cluereport .el-drawer__wrapper{
  width: 100%;
  height: 100%;
  position: fixed !important;
  overflow: hidden;
}
  .unitbox .el-table th{
    font-weight: normal;
    background-image: linear-gradient(to bottom , #f8f8f8, #efefef);
  }
  .unitbox .el-table td, .el-table th{
    padding:0;
    font-size:var(--size14) !important;
    line-height: 20px;
    font-family: '宋体'
  }
  .tableth{
    background-image: linear-gradient(to bottom , #f8f8f8, #efefef);
    height:33px;
  }
  #testlist .wboxs{
    height:45px;
    padding-top:10px;
  }
  #cluereport .el-table td{
    border-right: 1px solid #95B8E7;
    border-bottom: 1px solid #95B8E7;
  }
</style>
<style scoped>
  #cluereport .el-button{
    color:#fff !important;
  }
  #cluereport h1{
    font-size:var(--size16) !important;
    font-weight: bolder;
    height: 30px;
    border-bottom: 1px solid #95B8E7;
    font-family: '宋体';
    line-height: 30px;
    color: #000;
    padding-left:10px;
    background-image: linear-gradient(to bottom , #EEF4FF, #E0ECFF);
    margin:0;
  }
  .el-pagination{
    float: left;
    padding:7px 10px;
  }
  #cluereport .wboxs{
    width: 100%;
    background: #fff;
    border: 1px solid #95B8E7;
    margin:0 10px;
    overflow: hidden;
    padding:0;
  }
  .unitbox{
    padding:0;
    width: calc(100% - 0px);
    background: #fff;
    border: 1px solid #95B8E7;
    margin:0 10px;
    overflow: hidden;
  }
</style>
